/**
 * @param {number[][]} image
 * @param {number} sr
 * @param {number} sc
 * @param {number} color
 * @return {number[][]}
 */
var floodFill = function (image, sr, sc, color) {
  let m = image.length
  let n = image[0].length
  let oldColor = image[sr][sc]
  let dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]]
  function dfs (i, j) {
    if (image[i][j] === oldColor) {
      image[i][j] = color
    }
    for (let [di, dj] of dirs) {
      ni = i + di
      nj = j + dj
      if (ni < 0 || ni >= m || nj < 0 || nj >= n || image[ni][nj] !== oldColor) {
        continue // 如果越界 或者不是这个color 就不加入
      }
      dfs(ni, nj)
    }
  }
  dfs(sr, sc)
  return image
};
let image = [[0, 0, 0], [0, 0, 0]], sr = 0, sc = 0, color = 2
console.log(floodFill(image, sr, sc, color))